package com.ncf.rpc.common;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
/**
 * 编码器
 *  主要做数据通信编码
 */
@SuppressWarnings("rawtypes")
public class NCFEncoder extends MessageToByteEncoder{
	
	private Class<?> genericClass;
	
	public NCFEncoder(Class<?> genericClass){
		this.genericClass = genericClass;
	}

	@Override
	protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out)
			throws Exception {
		//序列化
		if(genericClass.isInstance(msg)) {
			byte[] data = SerializationUtil.serialize(msg);
			out.writeInt(data.length);
			out.writeBytes(data);
		}
		
	}

}
